<?php

namespace app\admin\command;

use app\common\library\job\TeamCountExpertLevelJob;
use app\common\library\job\TeamCountJob;
use app\common\library\TeamCount;
use app\common\library\token\driver\Redis as DriverRedis;
use app\common\library\User;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\Log;
use think\cache\driver\Redis;
use think\Queue;


class MonthOrderflowScore extends Command
{
    // 休眠间隔
    protected $_sleep = 30;
    // 是否有数据正在处理
    protected $_doing = false;
    //执行任务数组
    protected $_tasks = [];
    //缓存key
    protected $cache_key = "";

    protected function configure()
    {
        $this->setName('MonthOrderflowScore')->setDescription('每月订单流水分红');
    }

    /**
     * 休眠
     *
     * @param string $second            
     */
    public function sleep($second = "")
    {
        $second = $second ? $second : $this->_sleep;
        sleep(sleep($second)); // TP5的命令行 sleep($second) 不生效
        echo "sleep {$second} seconds ok!time:" . date('Y-m-d H:i:s') . PHP_EOL;
    }


    protected function execute(Input $input, Output $output)
    {
        $this->autoRun();
    }

    /**
     * 自动执行
     *
     * @return bool
     */
    protected function autoRun()
    {
        $this->getWorks();
        if ($this->_tasks) {

            $month = date("Ym");
            foreach ($this->_tasks  as $val) {
                // exit(print_r($user));
                $percent = 100;
                $item = db("user_weighting_score_detail")->where("user_id", $val["id"])->where("type", 2)->where("month", $month)->find();
                if (!$item) {
                    $data = [
                        "user_id" => $val["id"],
                        "level" => 0,
                        "type" => 2,
                        "percent" => $percent,
                        "month" => $month,
                        "add_time" => time(),
                    ];
                    db("user_weighting_score_detail")->insert($data);
                }
            }
        }

        echo "finished all" . PHP_EOL;
    }

    /**
     * 获取任务
     */
    protected function getWorks()
    {
        try {
            $list = db("user")->where("area_id", ">", 0)->select();
            return $this->_tasks = $list ?? [];
        } catch (Exception $e) {
            Log::write($e->getMessage());
        }
    }
}
